************************************************************************************************************************
*This is a replication do-file of randomization analyses for "Antidote to Backsliding: Ethnic Politics and Democratic Resilience" published in the APSR, (Figure A3)
*author: Jan Rovny, jan.rovny@sciencespo.fr 
*November 13, 2022 
*compiled with Stata MP 16.1 
*requires "eststo", "plottig"  ado files 
************************************************************************************************************************


clear all	
 
set seed 1234
local N=1000 // set iterations 

*create matrix to capture results
matrix Beta =J(`N',4,.) // create empty matrix with N rows, 4 cols, and filled with missing data . 
matrix colnames Beta = b_rr_eth b_rr_lib b_rr_comb b_cgov // name columns of matrix 

*start loop 
forvalues i=1/`N' {

*open vote share data 
use "Randomization.dta", clear

****randomization  
sample 97 if lib==1 // select 97% of lib parties (i.e. drop 3% of lib obs)
sample 97 if rr==1 // select 97% of rr parties (i.e. drop 3% of rr obs)

****
*collapse

collapse (sum) vote_share, by(cid year eth rr lib)

gen id=0
replace id=1 if rr==1 // rr
replace id=2 if eth==1  // eth 
replace id=3 if lib==1  // lib

*reshape 
drop rr eth lib
reshape wide vote_share, i(cid year) j(id)

rename vote_share0 vote_other
rename vote_share1 vote_rr
rename vote_share2 vote_eth
rename vote_share3 vote_lib

*fill missing as 0 
recode vote_rr vote_eth vote_lib (.=0)

*remove early years 
drop if cid==6 & year==1990 // Latvia
drop if cid==7 & year==1990 // Lithuania
drop if cid==8 & year==1989 // Poland
drop if cid==11 & year==1990 // Slovenia


gen elect=1 // idenfity election years 


*Merge in other variables 
merge 1:1 cid year using "Backsliding_data.dta", force keepusing(v2x_libdem col_gov year nygdppcapppkd_i gini_disp_i wdi_unempilo_i icrg_qog_i eu_mem)


*create interelection years
foreach var of varlist vote_other vote_rr vote_eth vote_lib {
	bysort cid (year): replace  `var' = `var'[_n-1] if `var' == .
}
drop if vote_eth==.
replace elect=0 if elect==.

*create combined ethnic and liberal vote
egen vote_comb = rowtotal(vote_eth vote_lib)


*create variable labels 
label var v2x_libdem "liberal democracy"
label var vote_rr "illiberal vote"
label var vote_eth "ethnic party vote"
label var vote_comb "combined ethnic \& lib vote"
label var vote_lib "const liberal vote"
label var col_gov "ethnic gov participation"
label var year "year"
label var nygdppcapppkd_i "GDP per capita"
label var gini_disp_i "gini"
label var wdi_unempilo_i "unemployment"
label var icrg_qog_i "quality of government"
label var eu_mem "EU membership"


eststo clear
tsset cid year

************************
*Analyses	
		
*Ethnic parties
xtreg v2x_libdem c.vote_rr##c.vote_eth col_gov year nygdppcapppkd_i gini_disp_i wdi_unempilo_i icrg_qog_i eu_mem, fe cl(cid)
matrix Beta[`i',1]=_b[c.vote_rr#c.vote_eth] // save beta for rr*eth 
matrix Beta[`i',4]=_b[col_gov] // save beta for col_gov
	
*Liberal parties 
xtreg v2x_libdem c.vote_rr##c.vote_lib col_gov year nygdppcapppkd_i gini_disp_i wdi_unempilo_i icrg_qog_i eu_mem, fe cl(cid)
matrix Beta[`i',2]=_b[c.vote_rr#c.vote_lib] // save beta for rr*lib 

*Combined 
xtreg v2x_libdem c.vote_rr##c.vote_comb col_gov year nygdppcapppkd_i gini_disp_i wdi_unempilo_i icrg_qog_i eu_mem, fe cl(cid)
matrix Beta[`i',3]=_b[c.vote_rr#c.vote_comb] // save beta for rr*comb 
}

matlist Beta 

clear 
svmat Beta // turn matrix to dataset 

*graph 
hist Beta1, xline(0, lpattern(solid) lcolor(red)) scheme(plottig) xlab(0(0.0002)0.0004) xsc(r(0 0.00041)) xti({&beta} of illib x ethnic vote) saving(b1.gph, replace)
hist Beta2, scheme(plottig) xline(0, lpattern(solid) lcolor(red)) xlab(0(0.00005)0.00011) xsc(r(0 0.00011)) xti({&beta} of illib x const. lib vote) saving(b2.gph, replace) // 
hist Beta3, scheme(plottig) xline(0, lpattern(solid) lcolor(red)) xlab(0(0.00005).000091) xsc(r(0 0.000091)) xti({&beta} of illib x combined vote) saving(b3.gph, replace) // 
hist Beta4, scheme(plottig) xline(0, lpattern(solid) lcolor(red)) xlab(0(0.01)0.05) xsc(r(0 0.048)) xti({&beta} of ethnic government participation) saving(b4.gph, replace)

graph combine b1.gph b2.gph b3.gph b4.gph, scheme(plottig) col(2) 
graph export Fig_A3.jpg, replace
